package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;


/**
 * Action: Sent by USR to unlock a Proposal which has been either locked
 * accidentaly e.g. when a network error has occurred after locking for edit.
 * NOTE: This TR can only be sent by the tool when a ProposalDownload fails 
 * due to a network error.
 * @author S.N.Fraser
 * $Id: UNLOCK_PROPOSALImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */
public class UNLOCK_PROPOSALImpl extends TransactionImpl implements Serializable {

    /** The path used to locate the Proposal in the OSS database. */
    protected Path proposalPath;
    
    /** The key to unlock the Proposal. */
    protected int key;

    /** Create an UNLOCL_PROPOSALImpl for the supplied request.*/
    public UNLOCK_PROPOSALImpl(UNLOCK_PROPOSAL request) {
  	super(request);
  	this.proposalPath = request.getProposalPath();
	this.key          = request.getKey();
    }


    /**
     * Unlocks the Proposal specified
     * @return Boolean or an OSS_Error.
     */
    public TRANSACTION_DONE exec() { 
	
	Proposal proposal = null;
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	    
	    // Locate the Proposal record from its path expression.
	    proposal = null;
	    try {
		proposal = OSS_Utilities.fetchProposal(proposalPath);
	    } catch (IOException iox) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal: "+proposalPath.toString()+
			   " Error fetching from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
				     "Proposal: "+proposalPath.toString()+
				     " Error reading from database: "+iox);
	    }		
	    

	    // Check it exists.
	    if (proposal == null) {
		logger.log(1, CLASS, id, "exec", 
			   "Could not locate Proposal: "
			   +proposalPath.toString());
		
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
				     "Could not find Proposal: "
				     +proposalPath.toString());
	    }
	
	    // Make sure the key will unlock the Proposal.
	    if (proposal.isLocked()) {
		logger.log(1, CLASS, id, "exec", 
			   "Proposal: "+proposal.getName()+" Lock: "+
			   proposal.getLock()+" Trying key: "+key);
		proposal.unLock(key);
		proposal.unLockChildren(key);
	    } else {		    
		// The Proposal isn't locked anyway.
		logger.log(1, CLASS, id, "exec", 
			   "Proposal: "+proposal.getName()+
			   " Attempt to unlock Proposal which has not been locked:");
		return error(TRANSACTION.DATABASE_OBJECT_NOT_LOCKED,
				     "Attempt to unlock Proposal which has not been locked: "
				     +proposal.getName());
	    }
	    
	    // Store it
	    try {
		OSS_Utilities.storeProposal(proposal);
	    } catch (IOException ix) {
		logger.log(1, CLASS, id, "exec",
			   "Proposal ["+proposal.getFullPath()+
			   "] Unable to Store: "+ix);
		return error(TRANSACTION.DATABASE_UPDATE_ERROR, 
				     "Proposal ["+proposal.getFullPath()+
				     "] Unable to Store: "+ix);
	    }	    	   
	    
	} finally {
	    t.commit();
	}
	
	UNLOCK_PROPOSAL_DONE done = new UNLOCK_PROPOSAL_DONE(id); 
	done.setSuccessful(true);
	return done;

    }
    
}

/** $Log: UNLOCK_PROPOSALImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/23 18:51:14  snf
/** Initial revision
/** */
